/// @tags: Trie
#include <cstdio>
#include <iostream>
#include <stack>

using namespace std;

namespace BlueQuantum {

constexpr int MAXN = 1e6 + 3, CHARSET = 2, BIT = 32, TRN = MAXN * BIT;

uint ch[TRN][CHARSET], end[TRN];

inline void insert(uint x, int len) {
  static uint tcnt = 1, icnt = 0;
  uint cur = 1;
  for (int i = BIT - 1; i >= BIT - len; --i) {
    int c = (x >> i) & 1;
    if (ch[cur][c] == 0) ch[cur][c] = ++tcnt;
    cur = ch[cur][c];
  }
  end[cur] = ++icnt;
}

inline int query(uint x, uint lim) {
  stack<uint> stk;
  uint cur = 1;
  for (int i = BIT - 1; i >= 0; --i) {
    int c = (x >> i) & 1;
    if (ch[cur][c] == 0) break;
    cur = ch[cur][c];
    if (end[cur] != 0 && end[cur] <= lim) {
      while (!stk.empty() && end[cur] < stk.top()) stk.pop();
      stk.push(end[cur]);
    }
  }
  return stk.size();
}

inline int main() {
  uint m;
  cin >> m;
  char opt;
  for (uint x, ip, i = 1, l, r; i <= m; ++i) {
    cin >> opt;
    ip = 0;
    for (uint j = 0; j < 4; ++j) {
      cin >> x;
      cin.get();
      ip <<= 8;
      ip |= x;
    }
    switch (opt) {
      case 'A':
        cin >> x;
        insert(ip, x);
        break;
      case 'Q':
        cin >> l >> r;
        cout << query(ip, r) - query(ip, l - 1) << '\n';
    }
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P5768 [CQOI2016] 路由表.in", "r", stdin);
  freopen("P5768 [CQOI2016] 路由表.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
